<template>
  <div class="page-container">
    <pf-table
      ref="pfTable"
      v-model="form"
      :columns="columns"
      :form-items="formItems"
      @get-table-data="getTableData"
    >
      <template #approveStatusStr="{row}">
        <el-tag v-if="row.approveStatus === 1" type="success">{{ row.approveStatusStr }}</el-tag>
        <el-tag v-if="row.approveStatus === -1 || row.approveStatus === 2" type="warning">{{ row.approveStatusStr }}</el-tag>
        <el-tag v-if="row.approveStatus === 0" type="danger">{{ row.approveStatusStr }}</el-tag>
      </template>
      <template #handle="{row}">
        <el-button type="text" size="small" @click="showDetail(row)">详情</el-button>
        <el-button v-if="row.approveStatus === -1" type="text" size="small" @click="approveHandle(row)">审核</el-button>
      </template>
    </pf-table>
  </div>
</template>

<script>
import { getIndustryList } from '@/api/dictionary'
import { getProductList } from '@/api/approve'
import { APPROVE_STATUS } from '@/utils/enum'

export default {
  name: 'ProductList',
  data() {
    return {
      approveStatus: APPROVE_STATUS,
      industryList: [],
      form: {},
      loading: false
    }
  },
  computed: {
    columns() {
      const attrs = { 'min-width': '150px' }
      return [
        { label: '行业', prop: 'industryName', attrs },
        { label: '产品名称', prop: 'productName', attrs },
        { label: '地址', prop: 'address', attrs },
        { label: '产品类型', prop: 'productCategoryStr', attrs },
        { label: '申请人', prop: 'createUser', attrs },
        { label: '申请人手机号', prop: 'createUserPhone', attrs },
        { label: '申请时间', prop: 'createTime', attrs },
        { label: '审核状态', slot: 'approveStatusStr', attrs },
        { label: '操作', slot: 'handle', attrs: { width: '150px', fixed: 'right' }}
      ]
    },
    formItems() {
      return [
        {
          type: 'input',
          label: '产品名称',
          prop: 'productName'
        }, {
          type: 'input',
          label: '地址',
          prop: 'address'
        }, {
          type: 'input',
          label: '申请人',
          prop: 'applyUser'
        }, {
          type: 'input',
          label: '手机号',
          prop: 'applyUserPhone'
        }, {
          type: 'select',
          label: '所属行业',
          prop: 'industryCode',
          options: this.industryList,
          optProps: {
            label: 'industryName',
            value: 'industryCode'
          }
        }, {
          type: 'select',
          label: '审核状态',
          prop: 'approveStatus',
          options: this.approveStatus
        },
        {
          type: 'date',
          label: '申请时间',
          prop: 'dateRange',
          span: 8,
          attrs: {
            type: 'datetimerange'
          }
        }
      ]
    }
  },

  created() {
    this.getIndustryList()
  },

  methods: {

    // 获取行业列表
    getIndustryList() {
      getIndustryList().then(res => {
        this.industryList = res || []
      }).catch(() => {})
    },

    getTableData({ pageNo, pageSize, ...formQuery }, done) {
      this.loading = true
      const [createStartTime = '', createEndTime = ''] = formQuery?.dateRange || []
      const data = {
        pageNo,
        pageSize,
        createStartTime,
        createEndTime,
        ...formQuery
      }
      getProductList(data).then(res => {
        this.loading = false
        done({ data: res.data || [], total: res.count || 0 })
      }).catch(() => {
        done({ data: [], total: 0 })
        this.loading = false
      })
    },

    // 查看详情
    showDetail(row) {
      this.$router.push({
        path: '/approve/product/detail',
        query: {
          type: 'detail',
          id: row.id
        }
      })
    },

    // 审核
    approveHandle(row) {
      this.$router.push({
        path: '/approve/product/detail',
        query: {
          type: 'approve',
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
